package com.itheima.blackitheima;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class MyTreeSet2 {
    public static void main(String[] args) {
        //创建TreeSet集合------匿名内部类
        TreeSet<Teacher> ts=new TreeSet<>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                //初条件
                int result=o1.getAge()-o2.getAge();
                //次条件
                result=result==0?o1.getName().compareTo(o2.getName()):result;
                return result;
            }
        });
        //创建Teacher对象
        Teacher tea1=new Teacher("zhangsan",23);
        Teacher tea2=new Teacher("lisi",21);
        Teacher tea3=new Teacher("wangwu",25);
        Teacher tea4=new Teacher("limazi",20);
        ts.add(tea1);
        ts.add(tea2);
        ts.add(tea3);
        ts.add(tea4);
        //迭代器
        Iterator<Teacher> it = ts.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("==============");
        //超级for
        for (Teacher t : ts) {
            System.out.println(t);
        }
    }
}
